﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDStore3Tiers;

namespace CuaHangDiaNhac
{
	public partial class frmGetDSDN : DevComponents.DotNetBar.Office2007Form
	{
		XuLyForm XL = new XuLyForm();
		XuLyDiaNhac XLDN = new XuLyDiaNhac();
		XuLyBaiHat XLBH = new XuLyBaiHat();

		public frmGetDSDN()
		{
			InitializeComponent();
		}

		private void GetDSDN_Load(object sender, EventArgs e)
		{
			XL.loadTreeviewBaihat(tvDSBHS, XLBH.getList().OrderBy(b => b.TenBH).ToList<BaiHat>());
		}

		private void frmGetDSDN_Activated(object sender, EventArgs e)
		{
			if (this.Owner != null)
			{
				this.Owner.Enabled = false;
			}
		}

		private void frmGetDSDN_Deactivate(object sender, EventArgs e)
		{
			if (this.Owner != null)
			{
				this.Owner.Enabled = true;
			}
		}

		private void btnXoa_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnThem_Click(object sender, EventArgs e)
		{
			try
			{
				StoreCDDataContext cn = new StoreCDDataContext();
				cn.Connection.ConnectionString = getConfig.getConnection();
				XuLyDiaNhac xl = new XuLyDiaNhac();
				string dsNodeChecked = "";
				int i = 0;
				foreach (TreeNode n in tvDSBHS.Nodes)
					if (n.Checked)
					{
						if (dsNodeChecked == "") dsNodeChecked = n.Tag.ToString();
						else dsNodeChecked += "," + n.Tag.ToString();
						i++;
					}
				string[] dsNodeDuocchon = dsNodeChecked.Split(',');
				if (!string.IsNullOrEmpty(dsNodeDuocchon.First()))
				{
					bool sameTieude = true;
					foreach (string val in dsNodeDuocchon)
					{
						if (XLDN.TonTai(frmDanhSachBaiHat_DiaNhac.ma, val))
						{
							sameTieude = false;
							throw new Exception("Đĩa đã có bài hát này !\nBài hát: " + xl.TenBH(val) + "\n Ca sĩ: " + xl.Casi(val));
						}
					}
					if (sameTieude)
					{
						foreach (string val in dsNodeDuocchon)
						{
							BaiHat_DiaNhac BH_DIA = new BaiHat_DiaNhac();
							BH_DIA.MaDia = int.Parse(frmDanhSachBaiHat_DiaNhac.ma);
							BH_DIA.MaBH = int.Parse(val);

							cn.BaiHat_DiaNhacs.InsertOnSubmit(BH_DIA);
							cn.SubmitChanges();

						}
						DevComponents.DotNetBar.MessageBoxEx.Show("Thêm bài hát vào địa nhạc thành công");
					}
					this.Close();
				}
				else throw new Exception("Chưa chọn bài hát !");
			}
			catch (Exception ex)
			{
				DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message, "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
	}
}
